//f[i][j]： 状态表示：将a[1~i]变成b[1~j]的最小步骤数
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 15, M = 1010;
int f[N][N];
char str[M][N];
int n, m;

int edit_dist(char str[], char s[])
{
    int l1 = strlen(str + 1), l2 = strlen(s + 1);
    int len = max(l1, l2);
    for(int i = 0; i <= len; ++ i)
    {
        f[0][i] = i;
        f[i][0] = i;
    }
    for(int i = 1; i <= l1; ++i)
        for(int j = 1; j <= l2; ++j)
        {
            f[i][j] = min(f[i-1][j], f[i][j - 1]) + 1;
            f[i][j] = min(f[i][j], f[i - 1][j - 1] + (str[i] != s[j]));
        }
    return f[l1][l2];
}

int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; ++i)
        cin >> (str[i] + 1);
    while(m--)
    {
        int limit, res = 0;
        char s[N];
        cin >> (s + 1) >> limit;
        for(int i = 1; i <= n; ++i)
            if(edit_dist(str[i], s) <= limit)
                ++res;
        cout << res << endl;
    }
    return 0;
}

